import 'dart:convert';

import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';
import 'package:www.znpigai.teacher/global_store/state.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(WebState state, Dispatch dispatch, ViewService viewService) {
  InAppWebViewController _controller;

  Widget bottomBar() {
    var buttons = <Widget>[];
    if (state.done.isNotEmpty) {
      buttons.add(
        SizedBox(
          // 去编辑，交作业
          width: 80,
          child: FlatButton(
            padding: EdgeInsets.all(8),
            color: Colors.blue,
            textColor: Colors.white,
            child: Text(state.done),
            shape: RoundedRectangleBorder(
                side: BorderSide(
                  color: Colors.white,
                  width: 0,
                ),
                borderRadius: BorderRadius.circular(20)),
            onPressed: () {
              dispatch(WebActionCreator.onDone());
            },
          ),
        ),
      );
    }
    if (state.share) {
      // 微信调通前，关闭分享按钮
      buttons.add(
        SizedBox(
          width: 80,
          child: FlatButton(
            padding: EdgeInsets.all(8),
            color: Colors.blue,
            textColor: Colors.white,
            child: Text('去得瑟'),
            shape: RoundedRectangleBorder(
                side: BorderSide(
                  color: Colors.white,
                  width: 0,
                ),
                borderRadius: BorderRadius.circular(20)),
            onPressed: () {
              dispatch(WebActionCreator.onShare());
            },
          ),
        ),
      );
    }

    return Container(
      padding: EdgeInsets.all(10),
      color: Colors.black12,
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: buttons,
      ),
    );
  }

  var bottomHeight = (state.share || state.done.isNotEmpty) ? 80.0 : 0.0;

  return Scaffold(
      appBar: AppBar(
        title: Text(state.title ?? ''),
        centerTitle: true,
        actions: <Widget>[
          IconButton(
            icon: new Icon(Icons.close),
            tooltip: 'Closes application',
            onPressed: () => Navigator.pop(viewService.context),
          ),
        ],
      ),
      body: Stack(
        children: <Widget>[
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            bottom: bottomHeight,
            child: InAppWebView(
              // 避免重复请求
              initialUrl: state.postData == null ? state.url : "about:blank",
              initialHeaders: {'ticket': GlobalState.ticket},
              initialOptions: {},
              onWebViewCreated: (InAppWebViewController controller) {
                _controller = controller;
                // post
                if (state.postData != null) {
                  var postStr = '';
                  state.postData.forEach((k, v) {
                    postStr += '$k=$v&';
                  });
                  var url = state.url + '?ticket=${GlobalState.ticket}';
                  _controller.postUrl(url, utf8.encode(postStr));
                }
              },
              onLoadStart: (InAppWebViewController controller, String url) {
                print("started $url");
              },
              onProgressChanged:
                  (InAppWebViewController controller, int progress) {
                dispatch(WebActionCreator.progress(progress));
              },
              onLoadStop: (InAppWebViewController controller, String url) {
                _controller
                    .getTitle()
                    .then((title) => dispatch(WebActionCreator.title(title)));
              },
            ),
          ),
          Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: Offstage(
                offstage: state.progress == 100,
                child: SizedBox(
                  height: 2.0,
                  child: LinearProgressIndicator(
                    valueColor: new AlwaysStoppedAnimation<Color>(Colors.green),
//                value: 80,
//                    backgroundColor: Colors.green,
                  ),
                ),
              )),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            height: bottomHeight,
            child: bottomBar(),
          ),
        ],
      ));
}
